#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int maxDistance(string s, int k) {
        int y = 0, x = 0;
        int res = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == 'N') ++y;
            else if (s[i] == 'S') --y;
            else if (s[i] == 'E') ++x;
            else if (s[i] == 'W') --x;
            res = max(res, min(abs(x) + abs(y) + k * 2, i + 1));
        }
        return res;
    }
};